import { Column, Entity, OneToMany, Relation, Tree, TreeChildren, TreeParent } from 'typeorm';

import { CompleteEntity } from '~/common/entity/common.entity';

import { UserEntity } from '../../user/user.entity';

@Entity({ name: 'sys_dept' })
@Tree('materialized-path')
export class DeptEntity extends CompleteEntity {
  @Column()
  name: string;

  @Column({ nullable: true, default: 0 })
  orderNo: number;

  @TreeChildren({ cascade: true })
  children: DeptEntity[];

  @TreeParent({ onDelete: 'SET NULL' })
  parent?: DeptEntity;

  @OneToMany(() => UserEntity, (user) => user.dept)
  users: Relation<UserEntity[]>;
}
